package com.pshcp.patient.business;

import cn.hutool.core.date.DateUtil;
import com.external.common.WebServiceException;
import com.external.service.WsRequestService;
import com.external.ws.soap.entity.dto.PayRegConfirmDTO;
import com.external.ws.soap.entity.res.CancelRegisterPayRes;
import com.external.ws.soap.entity.res.CancelRegisterRes;
import com.external.ws.soap.entity.res.PayRegConfirmRes;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.pshcp.common.enums.PayBusinessEnum;
import com.pshcp.common.enums.ReCallHisEnum;
import com.pshcp.domain.model.OrderReg;
import com.pshcp.domain.model.PayInfo;
import com.pshcp.manager.messagenotification.MessageNotificationManager;
import com.pshcp.patient.tool.OrderRegTools;
import com.pshcp.service.OrderRegService;
import com.pshcp.service.PayInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 预约挂号单业务
 */
@Slf4j
@Service
public class OrderRegBusiness {

    @Autowired
    private WsRequestService wsRequestService;

    @Autowired
    private OrderRegService orderRegService;

    @Autowired
    private MessageNotificationManager messageNotificationManager;

    @Autowired
    private PayInfoService payInfoService;

    /**
     * 取消挂号订单
     * @param orderReg
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO cancelRegOrder(OrderReg orderReg) throws WebServiceException {
        // valid sch date before today
        if(orderReg.getSchDate().compareTo(DateUtil.today()) <= 0){
            return ResultBuilder.failure("10170006");
        }
        CancelRegisterRes cancelRegisterRes = wsRequestService.cancelRegister(OrderRegTools.buildWsCancelRegisterDTO(orderReg));
        ResultDTO checkRes = OrderRegTools.checkWsCancel(cancelRegisterRes);
        if(!checkRes.getSuccess()){
            return checkRes;
        }
        OrderRegTools.buildCancelRegInfo(orderReg, cancelRegisterRes);
        orderRegService.update(orderReg);
        //send cancel msg
        messageNotificationManager.sendMessageNotification(OrderRegTools.buildCancelOrderMessage(orderReg));
        return ResultBuilder.success();
    }
    /**
     * 挂号订单退号
     * @param orderReg
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultDTO refundRegOrder(OrderReg orderReg) throws WebServiceException {
        // valid sch date before today
        if(orderReg.getSchDate().compareTo(DateUtil.today()) <= 0){
            return ResultBuilder.failure("10170004");
        }
        try{
            CancelRegisterPayRes cancelRegisterPay = wsRequestService.cancelRegisterPay(OrderRegTools.buildCancelRegisterPayDTO(orderReg));
            ResultDTO checkRes = OrderRegTools.checkWsRefund(cancelRegisterPay);
            if(!checkRes.getSuccess()){
                return checkRes;
            }
            OrderRegTools.buildRefundRegInfo(orderReg, cancelRegisterPay);
        }catch (WebServiceException e){
            log.error("call HIS refund fail, orderId:"+orderReg.getId(), e);
            OrderRegTools.buildWsRefundFail(orderReg);
        }
        orderRegService.update(orderReg);
        //send cancel msg
        if (!ReCallHisEnum.REFUND_FAIL.getCode().equals(orderReg.getReCallHis())){
            messageNotificationManager.sendMessageNotification(OrderRegTools.buildRefundOrderMessage(orderReg));
        }
        return ResultBuilder.success();
    }

    /**
     * 确认挂号订单
     * @param orderReg
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void enterRegOrder(OrderReg orderReg) throws WebServiceException {
        PayRegConfirmDTO payRegConfirmDTO = OrderRegTools.buildPayRegConfirmDTO(orderReg);
        //set third no
        PayInfo payInfo = payInfoService.findByBizIdAndPayBiz(orderReg.getId(), PayBusinessEnum.REG_PAY.getCode());
        if(payInfo != null){
            payRegConfirmDTO.setThirdTradeNo(payInfo.getThirdNo());
        }
        PayRegConfirmRes confirmRes = wsRequestService.payRegConfirm(payRegConfirmDTO);
        OrderRegTools.buildPayConfirmRes(orderReg, confirmRes);
        orderRegService.update(orderReg);
        //send msg
        messageNotificationManager.sendMessageNotification(OrderRegTools.buildRegOrderMessage(orderReg));
    }

}
